//
//  QitaViewController.m
//  ProJectB_1
//
//  Created by 布呐呐 on 16/8/19.
//  Copyright © 2016年 yangshukai. All rights reserved.
//

#import "QitaViewController.h"

@interface QitaViewController ()

@property (nonatomic, strong) NSMutableArray *pictureArr;

@end

@implementation QitaViewController

- (NSMutableArray *)pictureArr
{
    if (!_pictureArr) {
        _pictureArr = [NSMutableArray arrayWithCapacity:0];
    }
    return _pictureArr;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"PictureTableViewCell" bundle:nil] forCellReuseIdentifier:@"PictureTableViewCell"];
    [self getData];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    
//    [self json];
    // 添加退出按钮
    UIBarButtonItem *item = [[UIBarButtonItem alloc]initWithTitle:@"退出" style:(UIBarButtonItemStylePlain) target:self action:@selector(back:)];
    self.navigationItem.rightBarButtonItem = item;
}

- (void)back:(id)sender{
    DrawerViewController *menuController = (DrawerViewController*)((AppDelegate *)[[UIApplication sharedApplication] delegate]).rootTabBarViewController.drawerController;
    [menuController dismissViewControllerAnimated:YES completion:nil];
}

- (void)getData{
    [NetWorkRequestManager requestWithType:POST urlString:QiTaUrl parDic:nil finish:^(NSData *data) {
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingAllowFragments) error:nil];
        NSArray *arr = dict[@"list"];
        for (NSDictionary *dic in arr) {
            PictureModel *modle = [[PictureModel alloc]init];
            [modle setValuesForKeysWithDictionary:dic];
            [self.pictureArr addObject:modle];
            NSLog(@"================%@",modle.down);
        }
        [self.tableView reloadData];
    } error:^(NSError *error) {
        NSLog(@"数据解析失败");
    }];
}

//- (void)json {
//    NSURL *url = [NSURL URLWithString:@"http://s.budejie.com/topic/list/jingxuan/10/bs0315-iphone-4.3/0-20.json"];
//    
//    
//    NSURLSession *session = [NSURLSession sharedSession];
//    
//    NSURLSessionTask *task = [session dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
//        // 4、解析相关的数据
//        if (error == nil) {
//            NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
//            NSLog(@"%@", dict);
//        }
//    }];
//    [task resume];
//}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _pictureArr.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    PictureTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"PictureTableViewCell" forIndexPath:indexPath];
    PictureModel *model = self.pictureArr[indexPath.row];
    
    [cell setDataWithModel:model];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 400;
}


@end
